package com.zzl.bonds.ticket.service.common.impl;

import com.alibaba.fastjson.JSON;
import com.zzl.bonds.ticket.constant.StringConstant;
import com.zzl.bonds.ticket.constant.type.MsgCategoryEnum;
import com.zzl.bonds.ticket.constant.type.MsgTypeEnum;
import com.zzl.bonds.ticket.dto.common.MsgInfoDto;
import com.zzl.bonds.ticket.dto.feign.BathMsgDto;
import com.zzl.bonds.ticket.service.common.SendMsgService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.feign.params.MessageParams;
import com.zzl.general.common.serurity.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发送消息
 *
 * @author gull
 * @date 2021/12/7 10:16:58
 */
@Service
@Slf4j
public class SendMsgServiceImpl implements SendMsgService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送 mq 消息
     *
     * @param category    消息类别 如普通信息,操作信息
     * @param content     发送内容
     * @param receiverIds 接收人id，多个逗号拼接
     * @param user        当前登录用户
     * @param orgIds      组织架构id，不为空时，消息发送给这个组织架构下的所有的人,发送时，将所有要发送的部门逗号拼接
     */
    @Override
    public void sendMsgToMq(String category, String content, List<String> receiverIds, User user, List<String> orgIds) throws
            SysException {
        log.info("开始发送rabbitMq消息receiverIds:{}" + content + receiverIds);
        if (CollectionUtils.isEmpty(receiverIds)) {
            return;
        }
        MessageParams params = new MessageParams();
        params.setType(MsgTypeEnum.MSG_TYPE_ORDINARY.getCode());
        params.setCategory(category);
        params.setContent(content);
        params.setSenderId(user.getId().toString());
        //是否是群发,0不是1是
        if (!CollectionUtils.isEmpty(receiverIds)) {
            if (receiverIds.size() > 1) {
                params.setIsMass(1);
                String join = StringUtils.join(receiverIds, ",");
                params.setReceiverId(join);
            } else {
                params.setIsMass(0);
                params.setReceiverId(receiverIds.get(0));
            }
        }
        //是否需要socket推送 0:不需要 1:需要
        params.setIsNeedSend(1);
        if (!CollectionUtils.isEmpty(orgIds)) {
            params.setIsMass(1);
            String orgId = StringUtils.join(orgIds, ",");
            params.setOrgId(String.valueOf(orgId));
        }
        params.setEnterpriseId(user.getEnterpriseId());
        rabbitTemplate.convertAndSend(StringConstant.PROCESS_EXCHANGE, StringConstant.PROCESS_ROUT_KEY, JSON.toJSONString(params));

    }

    /**
     * 批量发送 mq 消息,发送不同消息给不同人
     *
     * @param bathMsgDtoList 消息发送实体
     * @throws SysException 业务异常
     */
    @Override
    public void sendBathMsgToMq(List<BathMsgDto> bathMsgDtoList) throws SysException {
        if (CollectionUtils.isEmpty(bathMsgDtoList)) {
            throw new SysException("消息内容不能为空");
        }
        for (BathMsgDto x : bathMsgDtoList) {
            singleSendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), x.getContent(), x.getReceiverId(), x.getUser());
        }

    }

    /**
     * 发送 mq 消息
     *
     * @param category   消息类别 如普通信息,操作信息
     * @param content    发送内容
     * @param receiverId 接收人id
     * @param user       当前登录用户
     */
    @Override
    public void singleSendMsgToMq(String category, String content, String receiverId, User user) throws
            SysException {
        log.info("开始发送rabbitMq消息receiverId:{}" + receiverId);
        List<String> receiverIds = new ArrayList<>();
        receiverIds.add(receiverId);
        sendMsgToMq(category, content, receiverIds, user, null);

    }

    /**
     * 给申请人发站内信
     *
     * @param id        申请id
     * @param creatorId 申请人id
     */
    @Override
    public void sendMsgToApplicant(String id, String creatorId, String type, String msg, User user) throws SysException {
        MsgInfoDto msgInfoDto = new MsgInfoDto(id, type, msg, "查看");
        this.singleSendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), JSON.toJSONString(msgInfoDto), creatorId, user);
    }

    /**
     * 给所有审批人发消息
     *
     * @param id             申请id
     * @param receiveUserIds 审批人id集合
     * @param type           类型
     * @param msg            消息
     * @param user           发送人
     */
    @Override
    public void sendMsgToAllReviewer(String id, List<String> receiveUserIds, String type, String msg, User user) throws SysException {
        MsgInfoDto msgInfoDto = new MsgInfoDto(id, type, msg, "查看");
        this.sendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), JSON.toJSONString(msgInfoDto), receiveUserIds, user, null);
    }

    /**
     * 给所有公司部门人员发消息
     *
     * @param id   申请id
     * @param type 类型
     * @param msg  消息
     * @param user 发送人
     */
    @Override
    public void sendMsgToAllPerson(String id, List<String> orgIds, String type, String msg, User user) throws SysException {
        MsgInfoDto msgInfoDto = new MsgInfoDto(id, type, msg, "查看");
        this.sendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), JSON.toJSONString(msgInfoDto), new ArrayList<>(), user, orgIds);
    }

    /**
     * 给所有通知人发消息
     * 1、给申请人和审核人员发消息
     *
     * @param id             开立id
     * @param receiveUserIds 接收人
     */
    @Override
    public void sendMsgToAllNotifier(String id, List<String> receiveUserIds, String type, String msg, User user, List<String> orgIds) throws SysException {
        MsgInfoDto msgInfoDto = new MsgInfoDto(id, type, msg, "查看");
        this.sendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), JSON.toJSONString(msgInfoDto), receiveUserIds, user, orgIds);
    }

    /**
     * 给所有通知人发消息
     * 1、给申请人和审核人员发消息
     *
     * @param id             开立id
     * @param receiveUserIds 接收人
     */
    @Override
    public void sendMsgToAllNotifierWithBtnName(String id, List<String> receiveUserIds, String type, String msg, User user, List<String> orgIds, String btnName) throws SysException {
        MsgInfoDto msgInfoDto = new MsgInfoDto(id, type, msg, btnName);
        this.sendMsgToMq(MsgCategoryEnum.MSG_CATEGORY_ORDINARY.getCode(), JSON.toJSONString(msgInfoDto), receiveUserIds, user, orgIds);
    }
}
